﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.Server.TVEInteractionLibrary;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;
using TV4Home.WebInterface.UI.Code;

namespace TV4Home.WebInterface.UI
{
    public partial class LiveTV : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                ViewState.Clear();

            if (Request.QueryString["channelId"] != null)
                this.CurrentChannelID = Convert.ToInt32(Request.QueryString["channelId"]);
        }

        public override void UpdateUI()
        {
            LoadChannels();

            if (this.CurrentChannelID == 0)
            {
                this.pnTVControls.Visible = true;
                this.tvControlHost.Visible = false;
            }
            else
            {
                this.pnTVControls.Visible = false;
                this.tvControlHost.Visible = true;

                ScriptManager.RegisterStartupScript(this, typeof(Page), "flowplayer", "flowplayer('tvPlayer', 'MediaStreaming/flowplayer/flowplayer-3.1.5.swf', { clip: { url: '" + GetMediaSourceURL() + "', autoPlay: true, autoBuffering: true, bufferLength: 5, live: true, scaling: 'fit', seekableOnBegin: false } });", true);
            }
        }

        [WebMethod]
        public static string GetEncoderLog()
        {
            //TODO: needs rework, would retrieve complete output everytime
            return ""; //MediaStreaming.MediaManager.EncoderLog.Replace(Environment.NewLine, "<br/>");
        }

        private void LoadChannels()
        {
            ddlChannels.Items.Clear();

            foreach (WebChannelBasic c in WebService.TV4HomeWebService.ServiceInstance.GetChannelsBasic(1))
            {
                ddlChannels.Items.Add(new ListItem(c.DisplayName, c.IdChannel.ToString()));
            }

            if (this.CurrentChannelID != 0)
                ddlChannels.SelectedValue = this.CurrentChannelID.ToString();
        }

        private string GetMediaSourceURL()
        {
            if (this.CurrentChannelID == 0)  //TODO: fix
                return "";
            else
                return "MediaStreaming/GetLiveTVStream.ashx?id=" + this.CurrentChannelID;
        }

        #region ViewState Properties
        private int CurrentChannelID
        {
            get
            {
                if (ViewState["currentChannelID"] == null)
                    ViewState["currentChannelID"] = 0;

                return (int)ViewState["currentChannelID"];
            }
            set
            {
                ViewState["currentChannelID"] = value;
            }
        }
        #endregion

        protected void btnWatchChannel_Click(object sender, EventArgs e)
        {
            this.CurrentChannelID = Convert.ToInt32(ddlChannels.SelectedValue);

            UpdateUI();
        }
    }
}
